package com.ruoyi.student.service.impl;

import com.ruoyi.student.domain.StudentShowJudgment;
import com.ruoyi.student.mapper.StudentShowJudgmentMapper;
import com.ruoyi.student.service.IStudentShowJudgmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StudentShowJudgmentServiceImpl implements IStudentShowJudgmentService {

    @Autowired
    private StudentShowJudgmentMapper studentShowJudgmentMapper;

    @Override
    public Map<String, Integer> getAllFieldPermissions() {
        List<Map<String, Object>> resultList = studentShowJudgmentMapper.selectAllFieldPermissions();
        Map<String, Integer> permissionsMap = new HashMap<>();
        for (Map<String, Object> row : resultList) {
            String fieldCode = (String) row.get("key");
            // 关键：将 Boolean 转为 Integer（true→1，false→0）
            Boolean isChange = (Boolean) row.get("value");
            permissionsMap.put(fieldCode, isChange ? 1 : 0);
        }
        return permissionsMap;
    }

    @Override
    public boolean checkFieldChangeable(String fieldCode) {
        Boolean isChange = studentShowJudgmentMapper.selectIsChangeByFieldCode(fieldCode);
        // 若字段未配置，默认“可修改”；否则按 isChange 取值
        return isChange == null ? true : isChange;
    }




    // 新增：查询所有列表（前端表格数据）
    @Override
    public List<StudentShowJudgment> selectStudentShowJudgmentList() {
        // 注意：需在Mapper中补充selectList方法（或直接写SQL）
        // 补充Mapper的selectList方法（见下方说明）
        return studentShowJudgmentMapper.selectStudentShowJudgmentList();
    }

    // 新增：更新is_change状态
    @Override
    public boolean updateIsChange(String fieldCode, Boolean isChange) {
        // 调用Mapper更新，返回影响行数>0则成功
        return studentShowJudgmentMapper.updateIsChangeByFieldCode(fieldCode, isChange) > 0;
    }

}